package com.windea.study.kotlin.game.tankwar2.interfaces

import com.windea.study.kotlin.game.tankwar2.*
import com.windea.study.kotlin.game.tankwar2.enums.*

/**可移动*/
interface Movable : Entity {
	/**当前方向。*/
	val direction: Direction
	/**速度。*/
	val speed: Int
	
	/**判断移动的物体是否和阻塞物体发生碰撞，返回要碰撞的方向，如果为null，则说明没有发生碰撞。*/
	fun willCollision(other: Blockable): Direction? {
		//将要碰撞时做判断
		//未来的坐标
		var x = this.x
		var y = this.y
		when(direction) {
			Direction.Up -> y -= speed
			Direction.Down -> y += speed
			Direction.Left -> x -= speed
			Direction.Right -> x += speed
		}
		//和边界进行检测
		//越界判断
		return when {
			x < 0 -> Direction.Left
			x > GameConfig.width - width -> Direction.Right
			y < 0 -> Direction.Up
			y > GameConfig.height - height -> Direction.Down
			other.checkCollision(x, y, width, height) -> direction
			else -> null
		}
	}
	
	/**发生碰撞时的事件。*/
	fun onCollision(other: Blockable?, direction: Direction?, entities: MutableList<Entity>) {}
	
	/**移动时的事件。*/
	fun onMove(entities: MutableList<Entity>) {}
}
